package fl.ass.service.api;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.DateUtils;
import cn.fl.capital.constant.CapitalConstant;
import cn.fl.project.condition.PrjPrdPaySchDQueryCondition;
import cn.fl.project.constant.PrjQuatPrpsConstant;
import cn.fl.project.constant.PrjQuatPrpsFeeConstant;
import cn.fl.project.dto.PrjPrdPaySchDDTO;
import cn.fl.project.dto.PrjPrdPaySchMDTO;
import cn.fl.project.service.IPrjPrdPaySchDApiService;
import cn.fl.project.service.IPrjPrdPaySchMApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.api.IAssDealRedeemApi;
import fl.ass.facade.condition.AssDealRedeemQueryCondition;
import fl.ass.facade.constant.InterfaceConstants;
import fl.ass.facade.dto.AssDealRedeemDTO;
import fl.ass.facade.dto.MessageDTO;
import fl.ass.service.service.IAssDealRedeemService;

/**
 * 处置赎回API
 * @author zy
 *
 */
@Component
public class AssDealRedeemApi implements IAssDealRedeemApi {
	
	private static final Logger logger = LoggerFactory.getLogger(AssDealRedeemApi.class);
	
	@Autowired
	private IAssDealRedeemService assDealRedeemService;
	
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Autowired
    private IPrjPrdPaySchDApiService prjPrdPaySchDApiService;

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealRedeemApi#saveAssDealRedeem(fl.ass.facade.dto.AssDealRedeemDTO)
	 */
	@Override
	public MessageDTO<String> saveAssDealRedeem(AssDealRedeemDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
            Long id;
            if (StringUtils.isEmpty(dto.getId())) {
			    id = assDealRedeemService.add(dto);
            } else {
                id = assDealRedeemService.addWithId(dto);
            }
			response.setId(id);
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealRedeemApi#updateAssDealRedeem(fl.ass.facade.dto.AssDealRedeemDTO)
	 */
	@Override
	public MessageDTO<String> updateAssDealRedeem(AssDealRedeemDTO dto) {
		MessageDTO<String> response = new MessageDTO<String>();
		if(null == dto){
			response.setMessage("parameter：dto is can not be null");
		}
		try {
			boolean flag = assDealRedeemService.modify(dto);
			response.setSuccess(flag);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	/* (non-Javadoc)
	 * @see fl.ass.facade.api.IAssDealRedeemApi#findAssDealRedeemById(java.lang.Long)
	 */
	@Override
	public MessageDTO<AssDealRedeemDTO> findAssDealRedeemById(Long id) {
		MessageDTO<AssDealRedeemDTO> response = new MessageDTO<AssDealRedeemDTO>();
		if(null == id){
			response.setMessage("parameter：id is can not be null");
		}
		try {
			AssDealRedeemDTO dto = assDealRedeemService.selectById(id);
			SysDictionaryDTO dictionary = null;
			if(null != dto){
				//处理拖回标记
				if(StringUtils.isNotBlank(dto.getBackedFlagCd())){
					dictionary = sysDictionaryApiService.getCode(InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE, InterfaceConstants.BACK_FLAG_CD, dto.getBackedFlagCd());
					if(null != dictionary){
						dto.setBackedFlagCd(dictionary.getCodeNm());
					}
				}
				// 结清优惠金额1添加到赎回对象中
				PrjPrdPaySchDQueryCondition qc = new PrjPrdPaySchDQueryCondition();
                qc.setFeeClsCd(PrjQuatPrpsConstant.CODE_PRJ_QUAT_FEE_CLS_TYP_SETT);
				qc.setIsDel(BaseConstant.IS_YESNO_NO);
				qc.setFeeTypCd(PrjQuatPrpsFeeConstant.FEE_TYP_CD_207);
                qc.setPaySchId(dto.getPaySchId());
                List<PrjPrdPaySchDDTO> prjPrdPaySchDDTOS = this.prjPrdPaySchDApiService.searchPrjPrdPaySchDList(qc);
                if (CollectionUtils.isNotEmpty(prjPrdPaySchDDTOS)) {
                    dto.setDiscountAmt(prjPrdPaySchDDTOS.get(0).getFeeAmt().longValue());
                }
                response.setData(dto);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
	}

	@Override
	public MessageDTO<AssDealRedeemDTO> searchList(
			AssDealRedeemQueryCondition condition) throws BaseException {
		MessageDTO<AssDealRedeemDTO> response = new MessageDTO<AssDealRedeemDTO>();
		if(null == condition.getDetailId() && null == condition.getMasterId()){
			response.setMessage("parameter：ids is can not be null");
		}
		try {
			List<AssDealRedeemDTO> dtos = assDealRedeemService.searchList(condition);
			if(null != dtos){
				response.setDatas(dtos);
			}
			response.setSuccess(true);
		} catch (Exception e) {
            BaseLogUtils.newLogger("异常!" + e.getMessage()).setThrowable(e).info();
			response.setMessage(e.getMessage());
		}
		return response;
    }

    @Override
    public Map<String, Double> getCalcStlCstIRRAndDiscount(Long paySchId, Long stlAmt, Date stlStDt) throws BaseException {
        return this.assDealRedeemService.getCalcStlCstIRRAndDiscount(paySchId, stlAmt, stlStDt);
    }

    @Override
    public boolean saveStlCstIrrAndDiscountAmt(Long redeemId, Long paySchId, Double stlCstIrr, Double discountAmt, Long usrId) throws BaseException {
        return this.assDealRedeemService.saveStlCstIrrAndDiscountAmt(redeemId, paySchId, stlCstIrr, discountAmt, usrId);
    }
	
}
